package com.cy.pj.ajax.controller;

import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class AjaxController {
    //假设这是数据库表(一个map对象对应一行记录)
    private List<Map<String,String>> dbList=new ArrayList<>();
    private AjaxController(){
        Map<String,String> map=new HashMap<>();//这个map中存储一些城市信息
        map.put("id", "100");
        map.put("city", "beijing");
        dbList.add(map);
        map=new HashMap<>();//这个map中存储一些城市信息
        map.put("id", "101");
        map.put("city", "shanghai");
        dbList.add(map);
    }
    @PutMapping("/doAjaxPut")
    public String doUpdateObject(@RequestParam Map<String,String> paramMap){
        String id=paramMap.get("id");
        Iterator<Map<String,String>> it=dbList.iterator();
        while(it.hasNext()){
            Map<String,String> map=it.next();
            if(map.get("id").equals(id)){
                map.put("city", paramMap.get("city"));
                //....
            }
        }
        return "update ok";
    };
    @DeleteMapping("/doAjaxDelete/{id}")
    public String doDeleteObject(@PathVariable  String id){
        //基于迭代器执行删除操作
        Iterator<Map<String,String>> it=dbList.iterator();
        while(it.hasNext()){
            Map<String,String> map=it.next();
            if(map.get("id").equals(id)){
                it.remove();//基于迭代器执行删除操作
            }
        }
        return "delete ok";
    }
    //接收post请求的普通表单数据
    @PostMapping("/doAjaxPost")
    public String doSaveObject(@RequestParam Map<String,String> map){
        dbList.add(map);
        return "save ok";
    }
    //接收post请求方式json格式数据
    @PostMapping("/doAjaxPostJSON")
    public String doSaveJsonObject(@RequestBody Map<String,String> map){
        dbList.add(map);
        return "save json ok";
    }

    //多个url映射为一个方法
    @GetMapping(path={"/doAjaxGet/{city}","/doAjaxGet"})
    public List<Map<String,String>> doAjaxGet(@PathVariable(required = false)  String city){

        if(city==null||"".equals(city))return dbList;
        List<Map<String,String>> queryResult=new ArrayList<>();//基于此集合存储查询结果
        for(Map<String,String> map:dbList){
            if(map.get("city").contains(city)){
                queryResult.add(map);
            }
        }
        return queryResult;
    }
//    @RequestMapping("/doAjaxGet")
//    @ResponseBody
//    public List<Map<String,String>> doAjaxGet(){
//        return dbList;
//    }

    //通过ajax方法发送get请求,访问此方法.
    //方法的返回值要在页面上通过局部刷新的方式进行呈现.
    @GetMapping("/doAjaxStart")
    public String doAjaxStart() throws InterruptedException {
        Thread.sleep(3000);
        return "Response Result Of Ajax Get Request 01 ";
    }
}


/*
    //假设这个是用于存储数据的数据库
    private List<Map<String,Object>> dbList=new ArrayList<>();
    public AjaxController(){
        Map<String,Object> map=new HashMap<>();
        map.put("id","100");
        map.put("name","家用电器");
        map.put("parentId","null");//parentId为null则表示他是1级分类
        map.put("remark","电器相关等");
        map.put("6",new Date());
        dbList.add(map);
    }

    @GetMapping("/doAjaxGet")
    public List<Map<String,Object>> doAjaxGet(){
        return dbList;
    }
    @PostMapping("/doAjaxPost")
    public String doAjaxPost(@RequestParam Map<String,Object> map){
        map.put("createdTime",new Date());
        dbList.add(map);
        return "save ok";
    }
    @DeleteMapping("/doAjaxDelete")
    public String doAjaxDelete(String id){
        //获取迭代器对象，然后迭代list集合，找到id对应的元素，进行删除操作
        Iterator it = dbList.iterator();
        while(it.hasNext()){
            Map<String,Object> map=(Map<String, Object>)it.next();
            if(map.get("id").equals(id)){
                //dbList.remove(map);//这样删除会出现并发删除异常
                it.remove();//通过迭代器执行删除操作
            }
        }
        return "delete ok";
    }
    @PutMapping("/doAjaxUpdate")
    public String doAjaxUpdate(@RequestParam Map<String,Object> updateMap){
        Iterator it=dbList.iterator();
        while (it.hasNext()){
            Map<String,Object> map=(Map<String, Object>)it.next();
            if(map.get("id").equals(updateMap.get("id"))){
                map.put("name",updateMap.get("name"));
                map.put("remark",updateMap.get("remark"));
            }
        }
        return "update ok";
    }
    //通过ajax方法发送get请求，访问此方法
    //方法的返回值要在页面上通过局部刷新的方式进行呈现
    @GetMapping("/doAjaxStart")
    public String doAjaxStart() throws InterruptedException {
        Thread.sleep(3000);
        return "Response Result Of Ajax Get Request 01";
    }

}
*/
